package com.ttl.bc.ic.stkissue.db;

import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.DynaActionForm;


import com.ttl.bc.ic.finishgoods.bean.BCFinishGoodsBean;
import com.ttl.bc.ic.finishgoods.bean.BCFinishGoodsSubBean;

import com.ttl.bc.ic.item.BCItemBO;

import com.ttl.bc.ic.stkissue.bean.BCStkIssueBean;
import com.ttl.bc.ic.stkissue.bean.BCStkIssueSubBean;
import com.ttl.bc.master.issuetype.BCIssueTypeBean;

import com.ttl.bc.master.project.BCProjectBean;

import com.ttl.bc.processstock.bean.ProcessStockBean;
import com.ttl.bc.processstock.db.ProcessStockDAO;
import com.ttl.bc.stock.request2.action.BCStkIssue2Form;
import com.ttl.bc.stock.request2.bean.BCStkIssue2;
import com.ttl.bc.stock.request2.bean.BCStkIssue2ShowBean;
import com.ttl.bc.stock.request2.bean.BCStkIssueSub2;
import com.ttl.bc.stock.request2.db.BCStkIssue2DAO;

import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;



public class BCStkIssueBO {

	@SuppressWarnings("unchecked")
	public List find() throws Exception {
					
		String monthFind = null;//(String)request.getParameter("monthFind");
		String yearFind  = null;//(String)request.getParameter("yearFind");
//		String docNoFind = "";//(String)request.getParameter("docNoFind");
				
		String hql = "";
		if(monthFind==null || yearFind==null) {
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("MM");			
			monthFind = sdf.format(date);
			
			sdf = new SimpleDateFormat("yyyy");
			String yy="";
			if (Integer.parseInt(sdf.format(date))<2500) {
				yy = (""+Integer.parseInt(sdf.format(date))+543).substring(2,2);
			}else {
				yy = sdf.format(date).substring(2,2);
			}			
			yearFind = yy;					
		}		
		
		hql = "FROM BCStkIssue2 WHERE " +
				" docNo like '%"+yearFind+monthFind+"%' "+
				" and isCancel=0 and (docNo like 'F1%' or docNo like 'F2%') ORDER BY docNo desc";
		
		BCStkIssueDAO dao = new BCStkIssueDAO();
		List results = dao.find(hql,0);		

		if(results!=null) {
			
			ArrayList<BCStkIssue2ShowBean> al = new ArrayList<BCStkIssue2ShowBean>();
			
			for(int i=0; i<results.size(); i++) {
				BCStkIssue2 s2 = (BCStkIssue2)results.get(i);
				
				for(Object o: s2.getBcStkIssueSub2s()) {
					BCStkIssueSub2 sub2 = (BCStkIssueSub2)o; 			
					
					BCStkIssue2ShowBean bean = new BCStkIssue2ShowBean();
					bean.setDocNo(s2.getDocNo());
					bean.setDocDate(s2.getDocDate());
					bean.setMyDescription(s2.getMyDescription());
					bean.setItemCode(sub2.getItemCode());
					bean.setItemName(sub2.getItemName());					
					bean.setQty(sub2.getQty());					
					bean.setIttl_unitName(sub2.getIttl_unitName());															
					bean.setMode("");
					
					bean.setBillStatus(s2.getBillStatus());
					bean.setDepartCode(s2.getDepartCode());
					bean.setiTTL_deptPay(s2.getiTTL_deptPay());
					bean.setProjectCode(s2.getProjectCode());
					bean.setIttl_status(s2.getiTTL_status());
					al.add(bean);
					
					
				}				
			}
			results = al;
		}
		
		return results;
	}

	public static BCStkIssue2Form setForm(HttpServletRequest request) {
		
		BCStkIssue2Form stkIssue2Form = new BCStkIssue2Form();
		
		stkIssue2Form.setDocNo(request.getParameter("docNo"));
		stkIssue2Form.setDocDate(request.getParameter("docDate"));
		
		stkIssue2Form.setProjectCode(request.getParameter("projectCode"));
		stkIssue2Form.setPersonCode(request.getParameter("personCode"));
		
		stkIssue2Form.setiTTL_deptPay(request.getParameter("iTTL_deptPay"));
		try {

			stkIssue2Form.setMyDescription(new String((""+(String)request.getParameter("myDescription")).getBytes("ISO8859_1"),"UTF-8"));
						
		} catch(Exception ex){}
		try {	
			stkIssue2Form.setItemName1(new String((""+(String)request.getParameter("itemName1")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setUnitName1(new String((""+(String)request.getParameter("unitName1")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setDivision1(new String((""+(String)request.getParameter("division1")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		try {	
			stkIssue2Form.setItemName2(new String((""+(String)request.getParameter("itemName2")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setUnitName2(new String((""+(String)request.getParameter("unitName2")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setDivision2(new String((""+(String)request.getParameter("division2")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		try {	
			stkIssue2Form.setItemName3(new String((""+(String)request.getParameter("itemName3")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setUnitName3(new String((""+(String)request.getParameter("unitName3")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setDivision3(new String((""+(String)request.getParameter("division3")).getBytes("ISO8859_1"),"UTF-8"));
		} catch(Exception ex){}
		try {	
			stkIssue2Form.setItemName4(new String((""+(String)request.getParameter("itemName4")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setUnitName4(new String((""+(String)request.getParameter("unitName4")).getBytes("ISO8859_1"),"UTF-8"));
			stkIssue2Form.setDivision4(new String((""+(String)request.getParameter("division4")).getBytes("ISO8859_1"),"UTF-8"));			
															
		}catch (Exception e) {
			System.out.println("############ error:"+e.toString());
		}				
		
		stkIssue2Form.setDepartCode(request.getParameter("departCode"))	;
		
		//System.out.println("request.getParameter(departCode) = "+request.getParameter("departCode"));
		
		stkIssue2Form.setIssueType(request.getParameter("issueType"));						
						
		stkIssue2Form.setItemCode1(request.getParameter("itemCode1"));
		stkIssue2Form.setShortName1(request.getParameter("shortName1"));		
		stkIssue2Form.setUnitCode1(request.getParameter("unitCode1"));
		stkIssue2Form.setQty1(request.getParameter("qty1"));				

		stkIssue2Form.setItemCode2(request.getParameter("itemCode2"));
		stkIssue2Form.setShortName2(request.getParameter("shortName2"));
		stkIssue2Form.setUnitCode2(request.getParameter("unitCode2"));
		stkIssue2Form.setQty2(request.getParameter("qty2"));		
				
		stkIssue2Form.setItemCode3(request.getParameter("itemCode3"));
		stkIssue2Form.setShortName3(request.getParameter("shortName3"));
		stkIssue2Form.setUnitCode3(request.getParameter("unitCode3"));
		stkIssue2Form.setQty3(request.getParameter("qty3"));				
		
		stkIssue2Form.setItemCode4(request.getParameter("itemCode4"));
		stkIssue2Form.setShortName4(request.getParameter("shortName4"));
		stkIssue2Form.setUnitCode4(request.getParameter("unitCode4"));
		stkIssue2Form.setQty4(request.getParameter("qty4"));		
									

		
		return stkIssue2Form;
	}	
	
	
	


	@SuppressWarnings("unchecked")
	public BCStkIssue2Form approveShow(HttpServletRequest request,
			HttpServletResponse response) {
						
		@SuppressWarnings("unused")
		boolean editComplete = false;
		BCStkIssue2Form form = new BCStkIssue2Form();
		
		String docNo = (String)request.getParameter("docNo");		
		
		BCStkIssue2DAO dao = new BCStkIssue2DAO();	
		String hql = "FROM BCStkIssue2 h " +
				" WHERE upper(h.docNo) = '"+docNo.toUpperCase()+"' ";
				
		List results = dao.find(hql);

		if (results!=null) {
			
			for(int i=0; i<results.size(); i++) {				
				BCStkIssue2 s2 = (BCStkIssue2)results.get(i);
				
				form.setDepartCode(s2.getDepartCode());
				form.setDocNo(docNo);
				form.setDocDate(""+s2.getDocDate());				
				form.setIssueType(s2.getBcIssueTypeBean().getCode());
				form.setIssueName(s2.getBcIssueTypeBean().getName());
				form.setMyDescription(s2.getMyDescription());
				form.setOrderNo(s2.getiTTL_orderNo());
				form.setPersonCode(s2.getPersonCode());
				form.setProjectCode(s2.getProjectCode());								
				
				//form.setProjectCode(s2.getBcProjectBean().getCode());
				//form.setProjectName(s2.getBcProjectBean().getName());
				form.setiTTL_deptPay(s2.getiTTL_deptPay());
				
				int id = 0;
				
				for(Object o: s2.getBcStkIssueSub2s()) {
					BCStkIssueSub2 sub2 = (BCStkIssueSub2)o;
					id = sub2.getLineNumber();
					//System.out.println("->> id="+id);
					switch (id) {
					case 1:						
						form.setItemCode1(sub2.getItemCode());
						form.setItemName1(sub2.getItemName());
						form.setShortName1(sub2.getIttl_itemCode_ttl2());
						form.setWhCode1(sub2.getWhCode());
						form.setShelfCode1(sub2.getShelfCode());
						//form.setShelfName1(sub2.getBcShelf().getName());						
						form.setUnitCode1(sub2.getUnitCode());
						form.setUnitName1(sub2.getIttl_unitName());
						form.setQty1(""+sub2.getQty());
						form.setPrice1(""+sub2.getPrice());
						form.setAmount1(""+sub2.getAmount());
						form.setDivision1(sub2.getIttl_division());						
						form.setIttl_unitName1(sub2.getIttl_unitName());
						
						break;
					case 2:
						form.setItemCode2(sub2.getItemCode());
						form.setItemName2(sub2.getItemName());
						form.setShortName2(sub2.getIttl_itemCode_ttl2());
						form.setWhCode2(sub2.getWhCode());
						form.setShelfCode2(sub2.getShelfCode());
						//form.setShelfName2(sub2.getBcShelf().getName());						
						form.setUnitCode2(sub2.getUnitCode());
						form.setUnitName2(sub2.getIttl_unitName());
						form.setQty2(""+sub2.getQty());
						form.setPrice2(""+sub2.getPrice());
						form.setAmount2(""+sub2.getAmount());						
						form.setDivision2(sub2.getIttl_division());
						form.setIttl_unitName2(sub2.getIttl_unitName());
						
						break;
					case 3:
						form.setItemCode3(sub2.getItemCode());
						form.setItemName3(sub2.getItemName());
						form.setShortName3(sub2.getIttl_itemCode_ttl2());
						form.setWhCode3(sub2.getWhCode());
						form.setShelfCode3(sub2.getShelfCode());
						//form.setShelfName3(sub2.getBcShelf().getName());
						form.setUnitCode3(sub2.getUnitCode());
						form.setUnitName3(sub2.getIttl_unitName());
						form.setQty3(""+sub2.getQty());
						form.setPrice3(""+sub2.getPrice());
						form.setAmount3(""+sub2.getAmount());						
						form.setDivision3(sub2.getIttl_division());
						form.setIttl_unitName3(sub2.getIttl_unitName());
						
						break;		
					case 4:
						form.setItemCode4(sub2.getItemCode());
						form.setItemName4(sub2.getItemName());
						form.setShortName4(sub2.getIttl_itemCode_ttl2());
						form.setWhCode4(sub2.getWhCode());
						form.setShelfCode4(sub2.getShelfCode());
						//form.setShelfName4(sub2.getBcShelf().getName());
						form.setUnitCode4(sub2.getUnitCode());
						form.setUnitName4(sub2.getIttl_unitName());
						form.setQty4(""+sub2.getQty());
						form.setPrice4(""+sub2.getPrice());
						form.setAmount4(""+sub2.getAmount());						
						form.setDivision4(sub2.getIttl_division());
						form.setIttl_unitName4(sub2.getIttl_unitName());
						
						break;
						
					}	
				}				
			}			
		}
		System.out.println(form.getShortName1());;
		System.out.println(form.getShortName2());;
		System.out.println(form.getShortName3());;
		System.out.println(form.getShortName4());;
		return form;	
	}

	@SuppressWarnings("unchecked")
	public static BCStkIssue2 setBCStkIssue2(BCStkIssue2Form stkIssue2Form) {
		
		BCStkIssue2 issue2 = new BCStkIssue2();

		issue2.setProjectCode(stkIssue2Form.getProjectCode());
		issue2.setiTTL_status("1");
		issue2.setBillStatus(1);
		issue2.setIsCancel(0);
		issue2.setDocNo(stkIssue2Form.getDocNo());
		issue2.setCreatorCode(stkIssue2Form.getPersonCode());
		issue2.setDepartCode(stkIssue2Form.getDepartCode());	    	  						
		
		issue2.setDocDate(CommonUtils.convertStringToDate(stkIssue2Form.getDocDate()));
		
		issue2.setIsCompleteSave(1);
		issue2.setIssueType(stkIssue2Form.getIssueType());
		issue2.setiTTL_orderNo(stkIssue2Form.getOrderNo());
		issue2.setiTTL_deptPay(stkIssue2Form.getiTTL_deptPay());
		issue2.setPersonCode(stkIssue2Form.getPersonCode());
		issue2.setMyDescription(stkIssue2Form.getMyDescription());
		
		HashSet s = new HashSet();			
		
		if( stkIssue2Form.getItemCode1() != null) {
			BCStkIssueSub2 issueSub2 = new BCStkIssueSub2();			
			issueSub2.setDepartCode(stkIssue2Form.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());
			
			issueSub2.setIssueType(stkIssue2Form.getIssueType());			
			issueSub2.setItemCode(stkIssue2Form.getItemCode1());
			issueSub2.setItemName(stkIssue2Form.getItemName1());
			issueSub2.setIttl_itemCode_ttl2(stkIssue2Form.getShortName1());
			issueSub2.setQty(Float.valueOf(stkIssue2Form.getQty1()));
			issueSub2.setRemainQty(Float.valueOf(stkIssue2Form.getQty1()));
							
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(stkIssue2Form.getUnitCode1());
//			issueSub2.setBcitemUnit(itemU);	
			issueSub2.setUnitCode(stkIssue2Form.getUnitCode1());
			
			issueSub2.setWhCode("10");			
			issueSub2.setLineNumber(1);
			issueSub2.setIttl_unitName(stkIssue2Form.getUnitName1());
			issueSub2.setIttl_division(stkIssue2Form.getDivision1());			
			
//			BCShelfBean shelf = new BCShelfBean();
//			shelf.setCode("10");			
//			issueSub2.setBcShelf(shelf);
			issueSub2.setShelfCode("10");
			
			issueSub2.setBcStkIssue2(issue2);
			
			issueSub2.setIttl_status("1");
			s.add(issueSub2);
		}
		
		if(stkIssue2Form.getItemCode2() != null) {
			BCStkIssueSub2 issueSub2 = new BCStkIssueSub2();
			
			issueSub2.setDepartCode(stkIssue2Form.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());
			issueSub2.setIssueType(stkIssue2Form.getIssueType());			
			issueSub2.setItemCode(stkIssue2Form.getItemCode2());
			issueSub2.setItemName(stkIssue2Form.getItemName2());
			issueSub2.setIttl_itemCode_ttl2(stkIssue2Form.getShortName2());
			issueSub2.setQty(Float.valueOf(stkIssue2Form.getQty2()));
			issueSub2.setRemainQty(Float.valueOf(stkIssue2Form.getQty2()));
			
			//issueSub2.setUnitCode(stkIssue2Form.getUnitCode2());
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(stkIssue2Form.getUnitCode2());
//			issueSub2.setBcitemUnit(itemU);			
			issueSub2.setUnitCode(stkIssue2Form.getUnitCode2());
			
			issueSub2.setWhCode("10");			
			issueSub2.setLineNumber(2);
			issueSub2.setIttl_unitName(stkIssue2Form.getUnitName2());
			issueSub2.setIttl_division(stkIssue2Form.getDivision2());			
			//issueSub2.setShelfCode(stkIssue2Form.getShelfCode2());				
			//issueSub2.setShelfCode( request.getParameter("shelfCode2") );
//			BCShelfBean shelf = new BCShelfBean();
////			shelf.setCode(request.getParameter("shelfCode2"));
//			shelf.setCode("10");
//			issueSub2.setBcShelf(shelf);
			issueSub2.setShelfCode("10");
			
			issueSub2.setBcStkIssue2(issue2);
			
			issueSub2.setIttl_status("1");
			s.add(issueSub2);
		}		
		
		if( stkIssue2Form.getItemCode3() != null) {
			BCStkIssueSub2 issueSub2 = new BCStkIssueSub2();
			
			issueSub2.setDepartCode(stkIssue2Form.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());
			issueSub2.setIssueType(stkIssue2Form.getIssueType());			
			issueSub2.setItemCode(stkIssue2Form.getItemCode3());
			issueSub2.setItemName(stkIssue2Form.getItemName3());
			issueSub2.setIttl_itemCode_ttl2(stkIssue2Form.getShortName3());
			issueSub2.setQty(Float.valueOf(stkIssue2Form.getQty3()));
			issueSub2.setRemainQty(Float.valueOf(stkIssue2Form.getQty3()));			
			//issueSub2.setUnitCode(stkIssue2Form.getUnitCode3());
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(stkIssue2Form.getUnitCode3());
//			issueSub2.setBcitemUnit(itemU);	
			issueSub2.setUnitCode(stkIssue2Form.getUnitCode3());
//			issueSub2.setWhCode(stkIssue2Form.getWhCode3());
			issueSub2.setWhCode("10");			
			issueSub2.setLineNumber(3);
			issueSub2.setIttl_unitName(stkIssue2Form.getUnitName3());
			issueSub2.setIttl_division(stkIssue2Form.getDivision3());			
			//issueSub2.setShelfCode(stkIssue2Form.getShelfCode3());				
			//issueSub2.setShelfCode( request.getParameter("shelfCode3") );
//			BCShelfBean shelf = new BCShelfBean();
////			shelf.setCode(request.getParameter("shelfCode3"));
//			shelf.setCode("10");
//			issueSub2.setBcShelf(shelf);	
			issueSub2.setShelfCode("10");
			
			issueSub2.setBcStkIssue2(issue2);
			
			issueSub2.setIttl_status("1");
			s.add(issueSub2);
		}		
		
		if(stkIssue2Form.getItemCode4() != null) {
			BCStkIssueSub2 issueSub2 = new BCStkIssueSub2();
		
			issueSub2.setDepartCode(stkIssue2Form.getDepartCode());
			issueSub2.setDocDate(issue2.getDocDate());
			issueSub2.setIssueType(stkIssue2Form.getIssueType());			
			issueSub2.setItemCode(stkIssue2Form.getItemCode4());
			issueSub2.setItemName(stkIssue2Form.getItemName4());
			issueSub2.setIttl_itemCode_ttl2(stkIssue2Form.getShortName4());
			issueSub2.setQty(Float.valueOf(stkIssue2Form.getQty4()));
			issueSub2.setRemainQty(Float.valueOf(stkIssue2Form.getQty4()));
			
			//issueSub2.setUnitCode(stkIssue2Form.getUnitCode4());
//			BCItemUnitBean itemU = new BCItemUnitBean();
//			itemU.setCode(stkIssue2Form.getUnitCode4());
//			issueSub2.setBcitemUnit(itemU);
			issueSub2.setUnitCode(stkIssue2Form.getUnitCode4());
//			issueSub2.setWhCode(stkIssue2Form.getWhCode4());
			issueSub2.setWhCode("10");			
			issueSub2.setLineNumber(4);
			issueSub2.setIttl_unitName(stkIssue2Form.getUnitName4());
			issueSub2.setIttl_division(stkIssue2Form.getDivision4());			
			//issueSub2.setShelfCode(stkIssue2Form.getShelfCode4());				
			//issueSub2.setShelfCode( request.getParameter("shelfCode4") );
//			BCShelfBean shelf = new BCShelfBean();
////			shelf.setCode(request.getParameter("shelfCode4"));
//			shelf.setCode("10");
//			issueSub2.setBcShelf(shelf);
			issueSub2.setShelfCode("10");
			
			issueSub2.setBcStkIssue2(issue2);
			
			issueSub2.setIttl_status("1");
			s.add(issueSub2);
		}
		
		issue2.setBcStkIssueSub2s(s);		
		
		return issue2;
	}
	
	@SuppressWarnings("unchecked")
	public boolean approveConfirm(HttpServletRequest request,
			HttpServletResponse response) throws InterruptedException {
		
		String user   = (String)request.getSession().getAttribute("userName");
							
		BCStkIssue2Form stkIssue2Form = setForm(request);				
		
		BCIssueTypeBean issueType = new BCIssueTypeBean();
		issueType.setCode(request.getParameter("issueType"));
		
		BCProjectBean proj = new BCProjectBean();
		proj.setCode(stkIssue2Form.getProjectCode());
		
		// update สถานะของใบ พ.1 นั้น -> ถูกใช้เพื่อเบิกแล้ว billStatus = 1, และ ittl_status =1 เพื่อ checked การเบิก, ittl_status=0 ยังไม่ได้เบิก 
		BCStkIssue2 issue2 = setBCStkIssue2(stkIssue2Form);
		issue2.setBcIssueTypeBean(issueType);
		
		// เพื่อให้ค้างในช่องค้นหา docNo
		request.setAttribute("docNoFind", issue2.getDocNo());
		
		BCStkIssue2DAO dao = new BCStkIssue2DAO();				
		System.out.println("\n\n## BCStkIssue2DAO status:"+dao.update(issue2.getDocNo(),issue2));
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2DAO #mode :update "+
				  "#docNo:"+issue2.getDocNo());		

		// สร้างรายการใหม่ลงใน table: BCStkIssue 
		BCStkIssueBean issue = setBCStkIssue(request,issue2);
		BCStkIssueDAO dao2 = new BCStkIssueDAO();
		System.out.println("add stkissue: "+dao2.add(issue));

		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueDAO #mode :add "+
				  "#docNo:"+issue.getDocNo());
		
		// ใส่ข้อมูล itemCode รายการนั้นใน table: processStock 
		ProcessStockDAO p = new ProcessStockDAO();
    	Iterator it = issue.getBcStkIssueSubs().iterator();
    	
        while(it.hasNext()) {
        	BCStkIssueSubBean sub = (BCStkIssueSubBean)it.next();
        	
        	ProcessStockBean bean = new ProcessStockBean();
        	bean.setItemCode(sub.getItemCode());
        	bean.setDocDate(sub.getDocDate());
        	bean.setProcessFlag(0);
        	bean.setFlowStatus(1);
        	bean.setProcessType(14);
        	bean.setProcessCase(1);// สำหรับ BCStkIssue บันทึกเอกสารให้ =1, ลบเอกสารให้=3
        	
        	p.add(bean);
        	
    		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ProcessStockDAO #mode :add "+
  				  "#docNo:"+issue.getDocNo() + " #item:"+sub.getItemCode());        	
        }
        Thread.sleep(2000);
//####### START : แสดงรายการ itemCode และ stockQty ปัจจุบัน(หลังเบิกจ่าย)
        it = issue.getBcStkIssueSubs().iterator();
		//String hql = "FROM BCItemBean WHERE code in ('";
        String hql = "FROM View_BCStkLocation WHERE itemCode in ('";
		while(it.hasNext()) {
			BCStkIssueSubBean sub = (BCStkIssueSubBean)it.next();
			hql+= sub.getItemCode()+"','";
		}
		hql = hql.substring(0,hql.length()-2)+")";
		hql = hql + " and whCode=10 and shelfCode=10";
		System.out.println("\n\nhql= "+hql);
		new BCItemBO().setPage2(request, hql);        
        
//####### ENd : แสดงรายการ itemCode และ stockQty ปัจจุบัน(หลังเบิกจ่าย)
        
		
		
		
//##########################################  ทำส่วนยืนยันการรับของ เพื่อเอาเข้า finishGoods ดีกว่าจะให้ st จ่ายเข้าให้เอง ###########################         
        // TODO ทำตรวจสอบว่าเป็น finishGoods หรือไม่ ถ้าใช่ก็ add เพิ่มเข้าไป ใน BCFinishGoods,processStock 
		// สร้างรายการใหม่ลงใน table: BCStkIssue
        
//		BCFinishGoodsBean fgBean = setBCFinishGoods(request, issue);
//		BCFinishGoodsDAO dao3 = new BCFinishGoodsDAO();
//		if(fgBean.getBcFinishGoodsSubs().size()>0) {
//			System.out.println("## BCFinishGoodsDAO status:"+dao3.add(fgBean));
//			System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCFinishGoodsDAO #mode :add "+
//					  "#docNo:"+fgBean.getDocNo());			
//		}else {
//			System.out.println("## not add to BCFinishGoodsDAO");
//		}
//                                
//    	Iterator it2 = fgBean.getBcFinishGoodsSubs().iterator();    	
//        while(it2.hasNext()) {
//        	BCFinishGoodsSubBean sub = (BCFinishGoodsSubBean)it2.next();
//        	
//        	ProcessStockBean bean = new ProcessStockBean();
//        	bean.setItemCode(sub.getItemCode());
//        	bean.setDocDate(sub.getDocDate());
//        	bean.setProcessFlag(0);
//        	bean.setFlowStatus(1);
//        	bean.setProcessType(2);
//        	bean.setProcessCase(1);// สำหรับ BCFinishGoodsSub บันทึกเอกสารให้ =1, ลบเอกสารให้=3
//        	
//        	p.add(bean);
//        	
//    		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ProcessStockDAO #mode :add "+
//    				  "#docNo:"+fgBean.getDocNo() + " #item:"+sub.getItemCode());        	
//        }
//##########################################         
		return true;
	}

	@SuppressWarnings({ "unchecked", "unused" })
	private BCFinishGoodsBean setBCFinishGoods(HttpServletRequest request,BCStkIssueBean issue) {
		BCFinishGoodsBean fgBean = new BCFinishGoodsBean();
		fgBean.setDocNo(issue.getDocNo());
		fgBean.setDocDate(issue.getDocDate());
		fgBean.setDepartCode(issue.getDepartCode());
		fgBean.setPersonCode(issue.getPersonCode());
		fgBean.setMyDescription(issue.getMyDescription());
		fgBean.setSumOfAmount(0d);
		fgBean.setIsConfirm(0);
		fgBean.setGlFormat("");
		fgBean.setIsCancel(0);
		// เมื่อ ST ตัดจ่าย ให้เป็น 0 ก่อนใน BCFinishGoods  รอให้ จนท.ฝ่ายนั้นๆไป approve ยืนยันรับจะ update เป็น 1
		fgBean.setIsCompleteSave(1);
		fgBean.setProjectCode(issue.getProjectCode());
		
		fgBean.setCreatorCode((String)request.getParameter("stkIssuePersonCode"));				
		fgBean.setCreateDateTime(new Date());
		
		HashSet hashSet = new HashSet();
		
		Iterator it = issue.getBcStkIssueSubs().iterator();
		int i=0;
        while(it.hasNext()) {
        	BCStkIssueSubBean sub = (BCStkIssueSubBean)it.next();
        	
        	BCFinishGoodsSubBean fgSub = new BCFinishGoodsSubBean();
        	
        	// TODO ตรวจสอบเรื่อง itemCode ที่จะเอาเข้า table BCFinishGoodsSub
        	// ถ้า itemCode นั้นเป็นของ stock จะเอาเข้า line item BCFinishGoodsSub
        	// แต่ถ้าไม่เป็น เช่น g-06, จะไม่เอาเป็น line item เข้า BCFinishGoodsSub
        	if(isStockItem(sub.getItemCode())==true) {
        		
            	fgSub.setDocDate(sub.getDocDate());
            	fgSub.setDepartCode(sub.getDepartCode());
            	fgSub.setPersonCode(sub.getPersonCode());
            	fgSub.setItemCode(sub.getItemCode());
            	fgSub.setItemName(sub.getItemName());
            	fgSub.setQty(Double.valueOf(""+sub.getQty()));
//            	fgSub.setUnitCode(sub.getBcitemUnit().getCode());
            	fgSub.setUnitCode(sub.getUnitCode());
            	fgSub.setProjectCode(sub.getProjectCode());
            	fgSub.setIsCancel(0);
            	fgSub.setLineNumber(++i);        	
            	fgSub.setBcFinishGoods(fgBean);
        		
        		int dept = Integer.parseInt(issue.getDepartCode());
        		if (dept>=100) {
        			fgSub.setWhCode("10");
        			fgSub.setShelfCode(getShelf(dept, sub.getItemCode()));
        		}else {
        			fgSub.setWhCode("20");
        			fgSub.setShelfCode("");
        		}
        		
        		hashSet.add(fgSub);
        	}        	
        }
        
        fgBean.setBcFinishGoodsSubs(hashSet);
        
		return fgBean;
	}

	private String getShelf(int dept, String itemCode) {
		String shelf="";
		
		switch (dept) {
			case 100:	shelf="35";	break;//sp			
			case 200:
			case 210:
			case 211:
			case 220:	shelf="32";	break;//fw    		
			case 280:	shelf="37";	break;//wm								
			case 300:
			case 310:
			case 320:
			case 330:
			case 340:
			case 350:
			case 360:
	        	if(dept==310 && (itemCode.substring(0, 2).equals("2-") ||
	        					itemCode.substring(0, 2).equals("3-") )) {		            			            		        		
	        		shelf="12";
	        	}else {        		
	        		shelf="31";		            		
	        	}            		
				break;						
			case 410:	shelf="34";	break;//PM 									
			case 600:
			case 610:
			case 620:	shelf="33";	break;//GA															
			case 700:
	        	if(itemCode.substring(0, 2).equals("2-") ||
	    				itemCode.substring(0, 2).equals("3-") ) {		            			            		        		
	        		shelf="16";
	        	}else {        		
	        		shelf="38";//YD		            		
	        	}						
				break;
			case 800:	shelf="36";	break;//sw
																	
		}
		
		return shelf;
	}

	private boolean isStockItem(String itemCode) {
		
		if(itemCode!=null) {
			if(itemCode.substring(0, 2).equals("G-") ||  
				itemCode.substring(0, 2).equals("H-") || 
				itemCode.substring(0, 2).equals("I-") ||
				itemCode.substring(0, 2).equals("K-") || 
				itemCode.substring(0, 2).equals("L-") || 
				itemCode.substring(0, 2).equals("M-") || 
				itemCode.substring(0, 2).equals("N-") || 
				itemCode.substring(0, 2).equals("O-") || 
				itemCode.substring(0, 2).equals("P-") || 
				itemCode.substring(0, 2).equals("Q-") || 
				itemCode.substring(0, 2).equals("R-") ||
				itemCode.substring(0, 3).equals("TRE") ) {
				
				return false;
			}else {
				return true;
			}			
		}
		
		return false;
	}

	@SuppressWarnings("unchecked")
	private BCStkIssueBean setBCStkIssue(HttpServletRequest request,BCStkIssue2 issue2) {
		BCStkIssueBean issue = new BCStkIssueBean();
		
		issue.setDocNo((String)request.getParameter("stkIssueDocNo"));
		issue.setDocDate(CommonUtils.convertStringToDate((String)request.getParameter("stkIssueDocDate")));		
		issue.setDepartCode(issue2.getDepartCode());
		issue.setIssueType(issue2.getIssueType());
				
		try {
			issue.setMyDescription(new String((""+(String)request.getParameter("stkIssueMyDescription")).getBytes("ISO8859_1"),"UTF-8"));							
		}catch (Exception e) {
			
		}
		issue.setMyDescription(issue.getMyDescription() );
		
		issue.setPersonCode((String)request.getParameter("stkIssuePersonCode"));				
		
		issue.setSumOfAmount(new Float(0));
		
		issue.setGlFormat("");
		issue.setGlStartPosting(0);
		
		issue.setIsCompleteSave(1);
		issue.setIsConfirm(0);
		issue.setIsCancel(0);
		
		issue.setProjectCode(issue2.getProjectCode());
		issue.setCreatorCode((String)request.getParameter("stkIssuePersonCode"));				
		issue.setCreateDateTime(new Date());								
		
		HashSet s = new HashSet();		
		
		Iterator it = (issue2.getBcStkIssueSub2s()).iterator();
	    while(it.hasNext()) {
	    	BCStkIssueSub2 sub2 = (BCStkIssueSub2)it.next();
	       	//System.out.println("itemCode = "+sub2.getItemCode());
	    	BCStkIssueSubBean sub = new BCStkIssueSubBean();
	    	sub.setIssueType(sub2.getIssueType());
	    	sub.setItemCode(sub2.getItemCode());
	    	sub.setDocDate(issue.getDocDate());
	    	sub.setDepartCode(sub2.getDepartCode());
	    	
	    	try {
	    		int dept = Integer.valueOf(sub.getDepartCode());
	    		//System.out.println("sub.getItemCode().charAt(0) = "+sub.getItemCode().charAt(0));
	    		if(( sub.getItemCode().startsWith("2") || sub.getItemCode().startsWith("3")) && 
	    				//(dept>=400 && dept<500)  
	    				(dept==310 || dept==320 || dept==700)   ) {
	    			issue.setGlFormat("NOGLO");
	    		}
			} catch (Exception e) {
				// TODO: handle exception
			}
	    	
	    	
	    	sub.setPersonCode(issue.getPersonCode());
	    	sub.setWhCode(sub2.getWhCode());
	    	sub.setQty(sub2.getQty());
	    	sub.setRetQty(sub2.getRemainQty());
	    	sub.setProjectCode(sub2.getProjectCode());
	    	sub.setLineNumber(sub2.getLineNumber());
	    	sub.setItemName(sub2.getItemName());
	    	sub.setRefNo(issue2.getDocNo());
	    	
//	    	BCItemUnitBean unit = new BCItemUnitBean();
//	    	unit.setCode(sub2.getBcitemUnit().getCode());
//	    	sub.setBcitemUnit(unit);
	    	sub.setUnitCode(sub2.getUnitCode());
	    	
//	    	BCShelfBean shelf = new BCShelfBean();
//	    	shelf.setCode(sub2.getBcShelf().getCode());
//	    	sub.setBcShelf(shelf);
	    	sub.setShelfCode(sub2.getShelfCode());
	    	
	    	sub.setBcStkIssue(issue);
	    	
	    	s.add(sub);
	    }		
		issue.setBcStkIssueSubs(s);
		
		return issue;
	}

	@SuppressWarnings("unchecked")
	public ArrayList find(HttpServletRequest request) {
		
//		String userName = (String)request.getSession().getAttribute("userName");		
		String docNoFind = (String)request.getParameter("docNoFind");		
		String deptFind = (String)request.getParameter("deptFind");
		String monthFind = (String)request.getParameter("monthFind");
		String yearFind = (String)request.getParameter("yearFind");

		Date date = new Date();
		if(monthFind == null ) {			
			SimpleDateFormat sdf = new SimpleDateFormat("MM");			
			monthFind = sdf.format(date);
		}		
		
		if(yearFind==null) {			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			String yy="";
			if (Integer.parseInt(sdf.format(date))<2500) {
				yy = (""+Integer.parseInt(sdf.format(date))+543).substring(2,2);
			}else {
				yy = sdf.format(date).substring(2,2);
			}			
			yearFind = yy;						
		}
		
		if(deptFind == null || deptFind.equalsIgnoreCase("ALL")) {
			deptFind = "";
		}
		
		request.setAttribute("monthFind", monthFind);
		request.setAttribute("yearFind", yearFind);
		request.setAttribute("deptFind", deptFind);
		
		String hql = "";
		
		if (docNoFind==null || docNoFind.equals("")) {
			hql = "FROM BCStkIssue2 s  " +
						 "WHERE  " +
						 " s.docNo like '%"+deptFind+"-"+yearFind+monthFind+"%' "+
						 " and  (s.docNo like 'F1%' or s.docNo like 'F2%') "+
//						 " and s.isCancel =0 "+
						 " ORDER BY s.docNo desc";
		}else {
			hql = "FROM BCStkIssue2 s  " +
			 "WHERE  " +
			 " s.docNo like '%"+docNoFind.trim()+"%' "+
			 " and  (s.docNo like 'F1%' or s.docNo like 'F2%') "+
//			 " and s.isCancel =0 "+
			 "ORDER BY s.docNo desc";			
		}
		
//		System.out.println(hql);
		BCStkIssue2DAO dao = new BCStkIssue2DAO();
		ArrayList results = (ArrayList)dao.find(hql,0);

		if(results!=null) {			
			ArrayList<BCStkIssue2ShowBean> al = new ArrayList<BCStkIssue2ShowBean>();
			
			for(int i=0; i<results.size(); i++) {
				BCStkIssue2 s2 = (BCStkIssue2)results.get(i);
				
				for(Object o: s2.getBcStkIssueSub2s()) {
					BCStkIssueSub2 sub2 = (BCStkIssueSub2)o; 			
					
					BCStkIssue2ShowBean bean = new BCStkIssue2ShowBean();
					bean.setDocNo(s2.getDocNo());
					bean.setDocDate(s2.getDocDate());
					bean.setMyDescription(s2.getMyDescription());
					bean.setBillStatus(s2.getBillStatus());					
					bean.setItemCode(sub2.getItemCode());
					bean.setItemName(sub2.getItemName());					
					bean.setQty(sub2.getQty());					
					bean.setIttl_unitName(sub2.getIttl_unitName());
					bean.setIttl_status(s2.getiTTL_status());
					bean.setMode("");
					
					al.add(bean);
				}				
			}
			results = al;
		}
		
		return results;
	}

	@SuppressWarnings("unchecked")
	public BCStkIssueBean find(String docNo) {
		String hql = "FROM BCStkIssueBean WHERE docNo like '%"+docNo+"%'";
		//System.out.println("hql="+hql);
		
		BCStkIssueDAO dao = new BCStkIssueDAO();
		List al = dao.find(hql);
		
		if (al!=null && al.size()>0) {
			System.out.println("al!=null");
			return (BCStkIssueBean)al.get(0);
		}else {
			System.out.println("al==null");
			return null;
		}						
	}

	
	
	
	public boolean approveConfirmCancel(HttpServletRequest request,
			HttpServletResponse response) throws InterruptedException {
		String user 	 = (String)request.getSession().getAttribute("userName");
		
		BCStkIssue2Form stkIssue2Form = BCStkIssueBO.setForm(request);
		String hql = "FROM BCStkIssueBean s WHERE docNo like '%"+stkIssue2Form.getDocNo()+"%'";
		ArrayList<BCStkIssueBean> al = (ArrayList<BCStkIssueBean>)new BCStkIssueDAO().find(hql);
		
		boolean abc = new BCStkIssueDAO().approveConfirmCancel(request, response);
		Thread.sleep(2000);
		if(al!=null && al.size()>0){		
			BCStkIssueBean issue = (BCStkIssueBean)al.get(0);
//####### START : แสดงรายการ itemCode และ stockQty ปัจจุบัน(หลังเบิกจ่าย)
	        Iterator it = issue.getBcStkIssueSubs().iterator();
			String hql2 = "FROM View_BCStkLocation WHERE itemCode in ('";
			while(it.hasNext()) {
				BCStkIssueSubBean sub = (BCStkIssueSubBean)it.next();
				hql2+= sub.getItemCode()+"','";
			}
			hql2 = hql2.substring(0,hql2.length()-2)+") and whCode='10' and shelfCode='10' ";
			new BCItemBO().setPage3(request, hql2);        
	        
//####### ENd : แสดงรายการ itemCode และ stockQty ปัจจุบัน(หลังเบิกจ่าย)
		}
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueDAO #mode :delete "+
				  "#docNo:"+stkIssue2Form.getDocNo());
		
		return abc;	
	}
	
	@SuppressWarnings("unchecked")
	public boolean approveConfirmCancel2(HttpServletRequest request,
			HttpServletResponse response) throws InterruptedException {
				
		BCStkIssue2Form stkIssue2Form = setForm(request);				
		
		BCProjectBean proj = new BCProjectBean();
		proj.setCode(stkIssue2Form.getProjectCode());
		
		BCIssueTypeBean issueType = new BCIssueTypeBean();
		issueType.setCode(request.getParameter("issueType"));
		
		// update สถานะของใบ พ.1 นั้น -> รอเบิกแล้ว billStatus = 0, และ ittl_status =1 เพื่อ checked การเบิก, ittl_status=0 ยังไม่ได้เบิก(ยกเลิกเบิก) 
		BCStkIssue2 issue2 = setBCStkIssue2(stkIssue2Form);	
		issue2.setBcIssueTypeBean(issueType);
		issue2.setiTTL_status("0");
		issue2.setBillStatus(0);
		Iterator itSub = issue2.getBcStkIssueSub2s().iterator();
		while (itSub.hasNext()) {
			BCStkIssueSub2 issueSub2 = (BCStkIssueSub2) itSub.next();
			issueSub2.setIttl_status("0");
		}
		
		// เพื่อให้ค้างในช่องค้นหา docNo
		request.setAttribute("docNoFind", issue2.getDocNo());
				
		BCStkIssue2DAO dao = new BCStkIssue2DAO();
		dao.update(issue2.getDocNo(),issue2);
				
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssue2DAO #mode :update");
			
				
		// ลบรายการออกจาก table: BCStkIssue
		String hql = "FROM BCStkIssueBean s WHERE docNo like '%"+issue2.getDocNo()+"%'";
		
		BCStkIssueDAO dao3 = new BCStkIssueDAO();
		ArrayList al = (ArrayList)dao3.find(hql);
		
		if(al!=null && al.size()>0){		
			BCStkIssueBean issue = (BCStkIssueBean)al.get(0);
			// ใส่ข้อมูล itemCode รายการนั้นใน table: processStock			
			ProcessStockDAO p = new ProcessStockDAO();
	    	Iterator it = issue.getBcStkIssueSubs().iterator();
	    	
	        while(it.hasNext()) {
	        	BCStkIssueSubBean sub = (BCStkIssueSubBean)it.next();
	        	
	        	ProcessStockBean bean = new ProcessStockBean();
	        	bean.setItemCode(sub.getItemCode());
	        	bean.setDocDate(sub.getDocDate());
	        	bean.setProcessFlag(0);
	        	bean.setFlowStatus(1);
	        	bean.setProcessType(14);
	        	bean.setProcessCase(3);// สำหรับ BCStkIssue บันทึกเอกสารให้ =1, ลบเอกสารให้=3
	        	
	        	p.add(bean);
	        	
	    		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :ProcessStockDAO #mode :add "+
	  				  "#docNo:"+issue.getDocNo() + " #item:"+sub.getItemCode());        	
	        }	        
		}
        // ลบรายการใน BCStkIssue
		dao3.delete(hql);
		System.out.println("delete stkissue: ");
		Thread.sleep(2000);
		if(al!=null && al.size()>0){		
			BCStkIssueBean issue = (BCStkIssueBean)al.get(0);
//####### START : แสดงรายการ itemCode และ stockQty ปัจจุบัน(หลังเบิกจ่าย)
	        Iterator it = issue.getBcStkIssueSubs().iterator();
			String hql2 = "FROM BCItemBean WHERE code in ('";
			while(it.hasNext()) {
				BCStkIssueSubBean sub = (BCStkIssueSubBean)it.next();
				hql2+= sub.getItemCode()+"','";
			}
			hql2 = hql2.substring(0,hql2.length()-2)+")";
			//System.out.println("\n\nhql= "+hql2);
			new BCItemBO().setPage2(request, hql2);        
	        
//####### ENd : แสดงรายการ itemCode และ stockQty ปัจจุบัน(หลังเบิกจ่าย)
		}
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueDAO #mode :delete "+
				  "#docNo:"+stkIssue2Form.getDocNo());		 
         
		return true;
	}

	public boolean swap(HttpServletRequest request, ActionForm form) {
		
		DynaActionForm mForm  = (DynaActionForm)form;		
		String	docNo1  = mForm.get("docNo1").toString();
		String	docNo2	= mForm.get("docNo2").toString();
		
		String swapType = request.getParameter("param");
//		param=toStkRequest
		
		if(swapType.equalsIgnoreCase("toStkRequest")) {
			return new BCStkIssueDAO().swapToStkRequest(request, docNo1);
		}else {
			return new BCStkIssueDAO().swapToStkIssue2(request, docNo2);
		}

	}

	public void showFind(HttpServletRequest request) throws UnsupportedEncodingException {
		@SuppressWarnings("unused")
		String userName = (String)request.getSession().getAttribute("userName");			
		@SuppressWarnings("unused")
		String monthFind = (String)request.getParameter("monthFind");
		@SuppressWarnings("unused")
		String yearFind = (String)request.getParameter("yearFind");

		@SuppressWarnings("unused")
		int userLevel =  (Integer)request.getSession().getAttribute("userLevel");		

		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind.trim());
		}
		String preDocNo  = (String)request.getParameter("deptFind");
		request.setAttribute("deptFind", preDocNo);
		
		String dateFind = "";
		if(request.getParameter("dateFind")!=null) {
			dateFind = ((String)request.getParameter("dateFind"));
			dateFind = new String(dateFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("dateFind", dateFind.trim());
		}		
		String ittl_status = (String)request.getParameter("ittl_status2");
		request.setAttribute("ittl_status2", ittl_status);
		System.out.println("ittl_status="+ittl_status);
		
		
/*		if(monthFind==null) {			
			monthFind = CommonUtils.getCurrentDateFormat("MM");										
		}
		
		if(yearFind==null) {
			yearFind = CommonUtils.getCurrentDateFormat("yyyyy");
			
			if (Integer.parseInt(yearFind)<2500) {								
				yearFind = (""+(Integer.parseInt(yearFind)+543)).substring(2);			
			}else {
				yearFind = yearFind.substring(2);
			}			
		}
				
		request.getSession().setAttribute("monthFind", monthFind);
		request.getSession().setAttribute("yearFind", yearFind);*/
		
		@SuppressWarnings("unused")
		String hql = "";
		if(itemFind==null||itemFind.trim().equals("")) {
			hql = "FROM BCStkIssue2 s  ";// +
//					 "WHERE upper(s.creatorCode)='"+userName.toUpperCase()+"' " +
//					 "and s.docNo like '"+preDocNo+"-"+yearFind+monthFind+"%' "+
//					 "and s.isCancel =0 "+
//					 "ORDER BY s.docNo desc";			
		}else {
			hql = "FROM BCStkIssue2 s  " +
					 "WHERE " +
//					 "upper(s.creatorCode)='"+userName.toUpperCase()+"' " +
					 "s.docNo like '%"+itemFind.trim()+"%' ";//+					 
//					 "and s.docNo like '"+preDocNo+"-%"+docNoFind.trim()+"%' "+
//					 "and s.isCancel =0 "+
//					 "ORDER BY s.docNo desc";
		}		
		//System.out.println(hql);
		
		String hql2 = "FROM BCStkIssue2 WHERE docNo like '%%' ";
		if(itemFind!=null && !itemFind.trim().equals("")) {
			hql2 += " and docNo like '%"+itemFind.trim()+"%' ";
		}
		if(preDocNo!=null && !preDocNo.equals("")) {
			hql2 += " and docNo like '"+preDocNo+"%' ";
		}		
		if(dateFind!=null && !dateFind.trim().equals("")) {
			hql2 += " and docNo like '%"+dateFind+"%' ";
		}
		if(ittl_status!=null && !ittl_status.equals("all") ) {
			hql2 += " and ittl_status = '"+ittl_status+"' ";
		}		
		System.out.println(hql2);
		//Page.setPage(request, hql2);		
		Page.setPageForBCStkIssue(request, hql2);
	}
		
}
